﻿@using RooHui.Infrastructure.Entities.Entity;
@using RooHui.Tool;
@{
    ViewBag.Title = "ProcessStepList";
    Layout = "~/Views/Shared/_Base.cshtml";
    ProcessDef processDef = ViewBag.processDef; 
}
<input type="hidden" name="processdefID" id="processdefID" value="@processDef.ID" />
<table border="0" width="100%" style="margin-top: 5px">
    <tr>
        <td align="right" width="80px">当前流程:</td><td align="left"><span class="header  green" style="font-size:16px;font-weight:bold">@processDef.ProcessDefName</span></td>
        <td align="right">
            <button id="btnAdd" class="btn btn-primary btn-small" style="margin-right: 5px">
                新增逻辑
            </button>
            &nbsp;
            <button id="btnBack" class="btn btn-warning btn-small" style="margin-right: 20px">
                返回
            </button>
        </td>
    </tr>
</table>
<table id="processstepgrid" class="mmg">
    <tr>
        <th rowspan="" colspan="">
        </th>
    </tr>
</table>
<div id="pg" style="text-align: right;">
</div>
<div id="processstep" class="modal fade" data-backdrop="static" tabindex="-1" style="display: none;
    width: 400px">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            ×</button>
        <h3 class="modal-title" style="font-size: 17px; font-weight: bold" id="modaltitle">
            新增逻辑条件</h3>
    </div>
    @*<div class="modal-body" style="z-index:9999">*@
    <table border="0" cellpadding="0" cellspacing="0" style="margin: 10px">
        <tr>
            <td valign="middle" width="100px" align="right">
                当前节点:
            </td>
            <td>
                <select id="currentProcess" name="currentProcess" class="selectpicker" data-style="btn-info">
                </select>
                <span style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                下一节点:
            </td>
            <td>
                <select id="nextProcess" name="nextProcess" class="selectpicker" data-style="btn-info">
                </select>
                <span style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                逻辑条件:
            </td>
            <td>
                <input type="text" id="txtProcessStepName" placeholder="逻辑条件" class="col-xs-10 col-sm-5" /><span
                    style="color: Red">*</span>
            </td>
        </tr>
        <tr>
            <td valign="middle" align="right">
                &nbsp;
            </td>
            <td>
                <span id="lblmsg" style="color: Red">&nbsp;</span>
                <input type="hidden" name="processstepID" id="processstepID" value="0" />
            </td>
        </tr>
    </table>
    @*</div>*@
    <div class="modal-footer">
        <button id="btnSave" type="button" class="btn btn-primary">
            保存</button>
        <button type="button" data-dismiss="modal" class="btn btn-default">
            取消</button>
    </div>
</div>
@section tail{
    <script src="/content/plugins/mmgrid/mmGrid.js" type="text/javascript"></script>
    <script src="/content/plugins/mmgrid/mmPaginator.js" type="text/javascript"></script>
    <script type="text/javascript">
        var processDiv = $("#processstep");
        var cols = [
        { title: '逻辑条件', name: 'StepName', width: 300, align: 'center' },
        { title: '当前节点', name: 'CurrentProcessID', width: 100, align: 'center', renderer: function (val, item, index) {
                if(item["CurrentProcessID"]==-1){
                                return "开始";
                            } 
                            else
                            {
                            
                                return item["CurrentProcess"]["ProcessName"];
                            }
            }
        },
        { title: '下一节点', name: 'NextProcessID', width: 60, align: 'center', renderer: function (val, item, index) {
                if(item.NextProcessID==-1){
                                return "结束";
                            } 
                            else
                            { 
                                return item["NextProcess"]["ProcessName"];
                            } 
            }
        },
        { title: '状态', name: 'Status', width: 60, align: 'center', renderer: function (val, item, index) {
                if (val == 0) return '禁用';
                return '正常';
            }
        },
        { title: '操作', name: 'ID', width: 150, align: 'center', lockWidth: true, lockDisplay: true, renderer: function (val, item, index) {
                var html = '<button  class="btn btn-info btn-mini" onclick="grid_processstep_edit(\'' + item["StepName"] + '\',\'' + item["CurrentProcessID"] + '\',\'' + item["NextProcessID"] + '\',\'' + item["ID"] + '\')">编辑</button>';

                html = html + ' <button  class="btn btn-danger btn-mini" onclick="grid_processstep_changestate(\'' + item["ID"] + '\',-1)">删除</button>';
                return html;
            }
        }
    ];
        var mmg;
        var currentProcess;
        var nextProcess;
        $(function () {
            bindProcess();

            mmg = $('#processstepgrid').mmGrid({
                indexCol: true,
                indexColWidth: 25,
                height: $("body", parent.document).height() - 220,
                cols: cols,
                url: "/Process/GetProcessStepListByPageAndDefID",
                method: 'get',
                remoteSort: true,
                //items: items,
                //sortName: 'SECUCODE',
                //sortStatus: 'asc',
                multiSelect: false,
                checkCol: false,
                fullWidthRows: true,
                autoLoad: false,
                plugins: [
                        $('#pg').mmPaginator()
                    ]
            });
            mmg.setParm({ defID: $("#processdefID").val() });
            mmg.load();

            $("#btnBack").click(function () {
                location.href = decodeURIComponent("/Process/Index");
            });

            $("#btnAdd").click(function () {
                resetform();

                var modaltitle = $("#modaltitle");
                modaltitle.text("新增逻辑条件");

                processDiv.modal();
            });
            $("#btnSave").click(function () {

                $("#btnSave").text("保存中...");
                $("#btnSave").attr("disabled", true);
                save();

            });

            $("#btnSearch").click(function () {

                search();

            });
        });

        function bindProcess() {
            $.ajax({
                url: "/Process/GetProcessListByDefID",
                data: "defID="+@processDef.ID,
                type: "get",
                success: function (result) {
                    result = eval("(" + result + ")");
                    currentProcess = $("#currentProcess");
                    currentProcess.empty(); //清空select下拉框

                    for (var i = 0; i < result.length; i++) {
                        $("<option value='" + result[i].ID + "'>" + result[i].ProcessName + "</option>").appendTo(currentProcess)//动态添加Option子项
                    }
                    currentProcess = currentProcess.selectpicker('show');
                }
            });

             $.ajax({
                url: "/Process/GetProcessListByDefID",
                data: "defID="+@processDef.ID+"&type=1",
                type: "get",
                success: function (result) {
                    result = eval("(" + result + ")");
                    nextProcess = $("#nextProcess");
                    nextProcess.empty(); //清空select下拉框

                    for (var i = 0; i < result.length; i++) {
                        $("<option value='" + result[i].ID + "'>" + result[i].ProcessName + "</option>").appendTo(nextProcess)//动态添加Option子项
                    }
                    nextProcess = nextProcess.selectpicker('show');
                }
            });
        }

        function search() {
            mmg.setParm({ defID: $("#processdefID").val() });
            mmg.load({ page: 1 });
        }

        function save() {
            if ($('#txtProcessStepName').val() == "") {
                $("#btnSave").text("保存");
                $("#btnSave").attr("disabled", false);
                $('#txtProcessStepName').focus();
                $("#lblmsg").html('逻辑条件不能为空');
                return;
            }

            $.ajax({
                url: "/Process/ProcessStepSave",
                data: "processDefID=" + $("#processdefID").val() + "&stepName=" + $('#txtProcessStepName').val()
                    + "&currentProcessID=" + currentProcess.val() + "&nextProcessID=" + nextProcess.val() + "&processStepID=" + $("#processstepID").val(),
                type: "post",
                success: function (result) {
                    //写提交ajax操作
                    if (result == "1") {
                        processDiv.modal("hide");
                        bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class=' icon-ok-sign bigger-290'></i>&nbsp;&nbsp;流程保存成功</span>", '300px', function () {

                            search();
                        });
                        resetform();
                    }
                    else {
                        if (result == "-1") {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-warning-sign bigger-290'></i>&nbsp;&nbsp;流程名已存在</span>", '300px');

                        }
                        else {
                            bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>&nbsp;&nbsp;流程保存失败</span>", '300px');
                        }
                    }
                    $("#btnSave").text("保存");
                    $("#btnSave").attr("disabled", false);
                }
            });
            //processDiv.modal("hide");
        }

        function grid_processstep_edit(processstepname, currentProcessID, nextProcessID, processstepId) {
            setfrom(processstepname, currentProcessID, nextProcessID, processstepId);

            var modaltitle = $("#modaltitle");
            modaltitle.text("编辑逻辑条件");
            processDiv.modal();
        }

        function grid_processstep_changestate(processstepID, state) {

            var statestr = "删除";

            bootbox.confirm("<span style='font-size:14px;'><i style='color:green;' class='icon-question-sign bigger-290'></i> &nbsp;&nbsp;确定要" + statestr + "该逻辑条件吗？</span>", '300px', function (confirmed) {
                if (confirmed == true) {
                    $.ajax({
                        url: '/process/ProcessStepChangeStatus',
                        data: "processStepID=" + processstepID + "&status=" + state,
                        type: "post",
                        cache: false,
                        success: function (result) {

                            if (result == "1") {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-ok-sign bigger-290'></i>&nbsp;&nbsp;" + statestr + "逻辑条件成功</span>", '300px');
                                mmg.load();
                            } else {
                                bootbox.alert("<span style='font-size:14px;'><i style='color:green;' class='icon-remove-sign bigger-290'></i>&nbsp;&nbsp;" + statestr + "逻辑条件失败</span>", '300px');

                            }
                        }
                    });
                }
            });

        }

        function resetform() {
            currentProcess.val("-1");
            currentProcess.selectpicker('render');
            nextProcess.val("-1");
            nextProcess.selectpicker('render');
            $("#txtProcessStepName").val("");
            $("#processstepID").val("0");
            $("#lblmsg").html("");
        }

        function setfrom(processstepname, currentProcessID, nextProcessID, processstepId) {
            currentProcess.val(currentProcessID);
            currentProcess.selectpicker('render');
            nextProcess.val(nextProcessID);
            nextProcess.selectpicker('render');
            $("#txtProcessStepName").val(processstepname);
            $("#processstepID").val(processstepId);
            $("#lblmsg").html("");
        }
    </script>
}
